package com.utils;

import java.util.ArrayList;
import java.util.List;

import com.entity.Cart;
import com.entity.Product;

public class CartUtil {
	private List<Cart> list = new ArrayList<Cart>();
	private double sum;

	public void setList(List<Cart> list) {
		this.list = list;
	}
	

	/**
	 * 获取所有商品
	 * @return
	 */
	public List<Cart> getList() {
		return list;
	}
	

	/**
	 * 新增购物车信息
	 * @param pro	商品对象
	 * @param stock	数量
	 * @return
	 */
	public ReturnResult addlist(Product pro ,int stock) {
		ReturnResult result = new ReturnResult();
		int row = 0;
		for(Cart cart : list){
		    if(cart.getProduct().getId() == pro.getId()) {
		    	if(cart.getStock()+stock > pro.getStock()) {
		    		row = 2;
		    		result.returnNo("商品数量不足");
		    	}else {
		    		cart.setStock(cart.getStock()+stock);
		    		row = 1;
		    	}
		    }
		}
		if(stock > pro.getStock()) {
			result.returnNo("商品数量不足");
		}
		if(row == 0) {
			Cart cart = new Cart(pro, stock);
			list.add(cart);
		}
		
		return result.returnYes();
	}
	
	/**
	 * 删除一条商品信息
	 * @param cart
	 */
	public void removeCart(Cart cart) {
		
	}
	
	/**
	 * 修改数量
	 * @param carts
	 */
	public void updateStock(Cart carts) {
		
	}
	
	//计算总价格
	public float getTotalCost() {
		float sum = 0;
		for (Cart item : list) {
			sum = sum + item.getPrice();
		}
		return sum;
	}
	
	public double getSum() {
		return sum;
	}


	public void setSum(double sum2) {
		this.sum = sum2;
	}

}

